const net = require('net');


//creat socket
const socket = new net.Socket({});

// 连接服务器

socket.connect({
    host:"localhost",
    port:4000
});

const lessonids = [
    "136797",
    "136798",
    "136799",
    "136800",
    "136801",
    "136803",
    "136804",
    "136806",
    "136807",
    "136808",
    "136809",
    "141994",
    "143517",
    "143557",
    "143564",
    "143644",
    "146470",
    "146569",
    "146582"
]


let id = Math.floor(Math.random()*lessonids.length)
console.log(lessonids[id]);

//
socket.write(encode(id));

socket.on('data',(buffer)=>{
    console.log(buffer.toString());

    // 接到数据之后，按照半双工通信逻辑，马上开始下一次请求
    id = Math.floor(Math.random()*lessonids.length)
    console.log(lessonids[id]);
    socket.write(encode(id));
})

// 编码逻辑

function encode(id){
    buffer = Buffer.alloc(4);
    buffer.writeInt32BE(
        lessonids[id]
    );
    return buffer ;
}